(function() {

  window.common = {};

  window.common.initialize = function() {
    $('.btn-group').button();
    $('.dropdown-toggle').dropdown();
    if (typeof OLD_IE !== "undefined" && OLD_IE !== null) {
      return $('.placeholder').clearField();
    }
  };

  $(function() {
    return common.initialize();
  });

}).call(this);

(function() {
  var get_language, initialize_app, initialize_control, load_class, login_app, login_control, login_fail, try_login;

  window.login = {};

  get_language = function(code) {
    switch (code) {
      case "jp":
        return "Japanese";
      default:
        return code;
    }
  };

  try_login = function() {
    if ($('#username-box').val() === '' || $('#password-box').val() === '') return;
    console.log("Sending: " + ($('#username-box').val()) + ", " + (hash($('#password-box').val())));
    return $.post('/users/validate', {
      u: $('#username-box').val(),
      p: hash($('#password-box').val())
    }, function(data) {
      if (data.app != null) {
        return login_app(data);
      } else if (data.control != null) {
        return login_control(data);
      } else {
        return login_fail(data);
      }
    });
  };

  login_app = function(data) {
    return $.get('/app/get', function(html) {
      $('body').html(html);
      return initialize_app(data);
    });
  };

  initialize_app = function(data) {
    initialize();
    $('#user-display').text(data.rn);
    if (!data.uc) {
      0;
    } else if (typeof data.uc === 'array') {
      0;
    } else {
      load_class(uc);
    }
    $('#login-box').modal('hide');
    $('.navbar').fadeIn();
    return $('#dojo').fadeIn();
  };

  login_control = function(data) {
    return $.get('/school/control/get', function(html) {
      $('body').html(html);
      return initialize_control(data);
    });
  };

  initialize_control = function(data) {
    return initialize(data);
  };

  login_fail = function() {
    return $.meow({
      message: "Username or password not valid!"
    });
  };

  load_class = function(uc) {
    login.language = uc.la;
    login["class"] = uc.cl;
    login.language_name = get_language(uc.la);
    return $.post('/data/get', {
      l: uc.la,
      c: uc.cl
    }, function(data) {
      vocab.load(data.v);
      grammar.load(data.g);
      activities.load(data.a);
      $('.lang-img').hide();
      return $("#" + uc.la + "-img").show();
    });
  };

  $(function() {
    $('#login-box').modal({
      backdrop: false,
      keyboard: false
    });
    return $('#login-btn').click(try_login);
  });

}).call(this);
